const express = require('express')
// const app = express()
const router = express.Router()
const db = require('./db.js')

// 该路由使用的中间件
/*router.use(function timeLog(req, res, next) {
  console.log('Time: ', new Date());
  next();
});*/

// 定义网站主页的路由
router.get('/', function (req, res) {
    res.send('Birds home page');
});

// 登录接口，并且验证密码--查询方法的使用案例

router.post('/login', function (req, res) {
    let name = JSON.stringify(req.body.username);
    let pwd = req.body.userpassword;
    let resData = { "name": name, "pass": pwd };
    let errText = '', resultData = '';
    db.selectAll('select * from ford_user_info where username = ' + name, (e, r) => {
        if (e) {
            res.status(200).json({ "status": false, "msg": e, "data": [] });
        }
        let tt = r.length;
        if (tt == 0) {
            errText = "账号不存在";
        }
        // console.log(tt,errText);
        res.status(200).json({ "status": true, "msg": errText, "data": resultData });
    })
    res.status(200).json({ "status": true, "msg": "true return", "data": resData });
});
// router.post('/register',(req,res)=>{
//     let  username = req.body.username;
//     let  userpassword = req.body.userpassword;
//     let errText = '',resultData='sccusee';
//     let saveData = {"username":username,"userpassword":userpassword};
//     db.insertData('ford_user_info',saveData,(e,r)=>{
//       if(e){
//           res.status(200).json({"status":false,"msg":e,"data":[]});
//       }
//       if(r) {
//           console.log('err')
//       }
//       res.status(200).json({"status":true,"msg":errText,"data":resultData});
//     })
//   })
//修改接口 req 请求 
// router.post('/modify',(req,res)=>{
//     let _where = {username : req.body.username};
//     let _set = {userpassword :req.body.userpassword};

//     db.updateData('ford_user_info',_set,_where,(e,r)=>{
//       if(e){
//         return  res.status(200).json({"status":false,"msg":e,"data":''});
//       }
//       let tt = r.length ;
//       if(tt == 0){
//         let errText="账号不存在";
//         console.log(errText + '123')
//       }
//       res.status(200).json({"status":true,"msg":'ok',"data":'resultData'});
//     })
//   })
module.exports = router;
